﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Web;

namespace QQ农场
{
    public partial class frmLogin : Form
    {
        private Stream Stream0;
        private Bitmap[] Bitmap0;
        private Bitmap Bitmap1;
        private int nCode = 4;
        private String strHtml;
        
        CookieContainer _cookieContainer = new CookieContainer();
        
        public frmLogin()
        {
            InitializeComponent();
        }

        private void but_Login_Click(object sender, EventArgs e)
        {
            Common com = new Common();
            Global gl = new Global();
            gl.Cookiecontailner = _cookieContainer;
            HttpHelper _help = new HttpHelper(_cookieContainer);
            string url = HttpUtility.UrlEncode("http://xiaoyou.qq.com/index.php?mod=login");
            //string url = "http://ptlogin2.qq.com/login";
            //string url = "";
            //url += "u=" + txt_QQNum.Text;
            //url += "&p=" + com.getMd5Hash2(com.getMd5Hash(txt_QQPass.Text).ToUpper() + txt_verifycode.Text.ToUpper()).ToUpper();
            //url += "&verifycode=" + txt_verifycode.Text.ToUpper();
            //url += "&aid=15000101";
            //url += "&u1=http://imgcache.qq.com/qzone/v5/loginsucc.html?para=izone";
            //url += "&fp=loginerroralert";
            //url += "&h=1";
            //url += "&ptredirect=1";
            //url += "&ptlang=0";
            //url += "&from_ui=1";
            //url += "&dumy=";
            /*
            WebRequest webRqst = WebRequest.Create(url);
            WebResponse webRpns = webRqst.GetResponse();
            Stream0 = webRpns.GetResponseStream();
            Encoding encode = Encoding.GetEncoding("gb2312");
            StreamReader sr = new StreamReader(Stream0, encode);
            char[] readbuffer = new char[256];
            int n = sr.Read(readbuffer, 0, 256);
            while (n > 0)
            {
                string str = new string(readbuffer, 0, n);
                strHtml += str;
                n = sr.Read(readbuffer, 0, 256);
            }
            */
            //string content = "u=" + this.txt_QQNum.Text + "&p=" + com.getMd5Hash2(com.getMd5Hash(this.txt_QQPass.Text).ToUpper() + this.txt_verifycode.Text.ToUpper()).ToUpper() + "&verifycode=" + this.txt_verifycode.Text + "&aid=15000101&u1=http://imgcache.qq.com/qzone/v5/loginsucc.html?para=izone&fp=loginerroralert&h=1&ptredirect=1&ptlang=0&from_ui=1&dumy=";
            string content = "u=" + this.txt_QQNum.Text + "&p=" + com.getMd5Hash2(com.getMd5Hash(this.txt_QQPass.Text).ToUpper() + this.txt_verifycode.Text.ToUpper()).ToUpper() + "&verifycode=" + this.txt_verifycode.Text + "&aid=15000101&u1=http%3A%2F%2Fimgcache.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&fp=loginerroralert&h=1&ptredirect=1&ptlang=0&from_ui=1&dumy=";
            //CookieContainer cc = new CookieContainer();
            //WebResponse response = com.doPost("http://ptlogin2.qq.com/login", content);
            //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //strHtml = reader.ReadToEnd();
            //strHtml = com.doPost("http://ptlogin2.qq.com/login", content);
            strHtml = _help.GetHtml("http://ptlogin2.qq.com/login", content, true, _cookieContainer);
            Console.WriteLine(strHtml.IndexOf("登录失败"));
            if (strHtml.IndexOf("登录失败") > 0)
            {
                MessageBox.Show("登录失败！");
                return;
            }
            this.Hide();
            frmMain fm = new frmMain();
            fm.Show();
            //Console.WriteLine(strHtml);
        }

        public Bitmap GetBitmapOriginal(ref Bitmap oriBmp)
        {
            HttpHelper _help = new HttpHelper(_cookieContainer);
            //WebRequest webRqst = WebRequest.Create("http://ptlogin2.qq.com/getimage?aid=15000101");
            //WebResponse webRpns = webRqst.GetResponse();

            //Stream0 = webRpns.GetResponseStream();
            Stream0 = _help.GetStream("http://ptlogin2.qq.com/getimage?aid=15000101", _cookieContainer);
            oriBmp = new Bitmap(Stream0);

            return oriBmp;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            Bitmap0 = new Bitmap[3 + nCode];
            Bitmap1 = GetBitmapOriginal(ref Bitmap0[0]);
            pictureBox1.Image = (Image)Bitmap1;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Bitmap1 = GetBitmapOriginal(ref Bitmap0[0]);
            pictureBox1.Image = (Image)Bitmap1;
        }

        private void but_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }


}